package linked_list;

import java.util.HashMap;
import java.util.Map;

public class 随机链表的复制 {

    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    //哈希表
    class Solution {
        public Node copyRandomList(Node head) {
            if(head == null){
                return null;
            }
            Node cur = head;
            Map<Node, Node> map = new HashMap<Node, Node>();
            while(cur != null){
                map.put(cur, new Node(cur.val));
                cur = cur.next;
            }
            cur = head;
            while(cur != null){
                map.get(cur).next = map.get(cur.next);
                map.get(cur).random = map.get(cur.random);
                cur = cur.next;
            }
            return map.get(head);
        }
    }
    //拼接 + 拆分
    class Solution2 {
        public Node copyRandomList(Node head) {
                    if(head == null) return null;
                    Node cur = head;
                    // 1. 复制各节点，并构建拼接链表
                    while(cur != null) {
                        Node tmp = new Node(cur.val);
                        tmp.next = cur.next;
                        cur.next = tmp;
                        cur = tmp.next;
                    }
                    // 2. 构建各新节点的 random 指向
                    cur = head;
                    while(cur != null) {
                        if(cur.random != null)
                            cur.next.random = cur.random.next;
                        cur = cur.next.next;
                    }
                    // 3. 拆分两链表
                    cur = head.next;
                    Node pre = head, res = head.next;
                    while(cur.next != null) {
                        pre.next = pre.next.next;
                        cur.next = cur.next.next;
                        pre = pre.next;
                        cur = cur.next;
                    }
                    pre.next = null; // 单独处理原链表尾节点
                    return res;      // 返回新链表头节点

            }

    }

}
